package st.business.tepth.business.user.busnumber.workholder.maintain.finish;

import android.app.Dialog;
import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.List;

import androidx.annotation.NonNull;
import st.business.tepth.R;
import st.business.tepth.business.bean.MaintainPassInfoBean;
import st.business.tepth.config.ItemFields;
import st.shadow.library.core.util.utilcode.util.ScreenUtils;
import st.shadow.library.core.util.utilcode.util.ToastUtils;
import st.shadow.library.ui.recycler.MultipleItemEntity;

/**
 * @author zhangyu
 * @date 2019/9/9
 */
public class DialogFinishNotPass extends Dialog {
    private BaseQuickAdapter<MultipleItemEntity, BaseViewHolder> mAdapter;

    public DialogFinishNotPass(@NonNull Context context,
                               String title,
                               final List<MultipleItemEntity> dataList,
                               final OnConfirmListener onConfirmListener) {
        super(context);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        View view = LayoutInflater.from(context).
                inflate(R.layout.dialog_finish_not_pass, null);
        TextView tvTitle = view.findViewById(R.id.tv_choose_action);
        tvTitle.setText(title);
        final EditText etReason = view.findViewById(R.id.et_search);
        view.findViewById(R.id.iv_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        view.findViewById(R.id.tv_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        view.findViewById(R.id.tv_confirm).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String ids = getCheckIds();
                if (ids.isEmpty()) {
                    ToastUtils.showShort("请选择分系");
                } else {
                    if (onConfirmListener != null) {
                        onConfirmListener.onConfirm(ids, etReason
                                .getText().toString());
                    }
                    dismiss();
                }
            }
        });
        final CheckBox cbAll = view.findViewById(R.id.cb_all);
        cbAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkAll(cbAll.isChecked());
            }
        });
        RecyclerView recyclerView = view.findViewById(R.id.recycler_view);


        recyclerView.setLayoutManager(new GridLayoutManager(context, 4));
        RecyclerView.ItemAnimator itemAnimator = recyclerView.getItemAnimator();
        if (itemAnimator != null) {
            itemAnimator.setChangeDuration(0);
        }
        recyclerView.setAdapter(mAdapter = new BaseQuickAdapter<MultipleItemEntity,
                BaseViewHolder>(R.layout.item_allocation_detail_type_1, dataList) {

            @Override
            protected void convert(BaseViewHolder helper, final MultipleItemEntity item) {
                final CheckBox checkBox = helper.getView(R.id.check_person);
                final MaintainPassInfoBean maintainPassInfoBean =
                        item.getField(ItemFields.ITEM_OBJ);
                checkBox.setText(maintainPassInfoBean.getVehicleSeriesName());
                checkBox.setChecked((Boolean) item.getField("isChecked"));
                helper.getView(R.id.layout_checkbox).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        boolean isChecked = item.getField("isChecked");
                        checkBox.setChecked(!isChecked);
                        item.setField("isChecked", !isChecked);
                        cbAll.setChecked(getCheckCount() == dataList.size());
                    }
                });
            }
        });
        setContentView(view);
    }

    private void checkAll(boolean isChecked) {
        if (mAdapter != null) {
            List<MultipleItemEntity> dataList = mAdapter.getData();
            for (MultipleItemEntity entity : dataList) {
                entity.setField("isChecked", isChecked);
            }
            mAdapter.notifyDataSetChanged();
        }
    }

    private int getCheckCount() {
        int count = 0;
        if (mAdapter != null) {
            List<MultipleItemEntity> dataList = mAdapter.getData();
            for (MultipleItemEntity entity : dataList) {
                if (entity.getField("isChecked")) {
                    count++;
                }
            }
        }
        return count;
    }

    private String getCheckIds() {
        StringBuilder ids = new StringBuilder();
        if (mAdapter != null) {
            List<MultipleItemEntity> dataList = mAdapter.getData();
            for (MultipleItemEntity entity : dataList) {
                if (entity.getField("isChecked")) {
                    MaintainPassInfoBean maintainPassInfoBean =
                            entity.getField(ItemFields.ITEM_OBJ);
                    ids.append(maintainPassInfoBean.getRepairTaskId()).append(",");
                }
            }
            if (ids.length() > 0) {
                ids = new StringBuilder(ids.substring(0, ids.length() - 1));
            }
        }
        return ids.toString();
    }

    public Dialog measureSize() {
        Window dialogWindow = getWindow();
        if (dialogWindow != null) {
            WindowManager.LayoutParams lp = dialogWindow.getAttributes();
            lp.width = ScreenUtils.getScreenWidth() * 1000 / 1280; // 宽度
            lp.height = ScreenUtils.getScreenHeight() * 800 / 1024; // 高度
            dialogWindow.setGravity(Gravity.CENTER);
            dialogWindow.setAttributes(lp);
        }
        return this;
    }

    public static class IDBean {
        private int id;

        public IDBean(int id) {
            this.id = id;
        }

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }
    }

    public interface OnConfirmListener {
        public void onConfirm(String ids, String reason);
    }
}
